/* $Id: SessionEntry.java 137 2011-08-29 20:33:36Z altdotua@gmail.com $ */

package alt.djudge.frontend.server.datatypes;

import java.util.Date;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import alt.djudge.frontend.shared.dto.admin.AdminDTOEntry;
import alt.djudge.frontend.shared.dto.admin.SessionEntryDTO;


@PersistenceCapable(detachable="true")
public class SessionEntry extends AbstractEntry
{
	private static final long serialVersionUID = 1L;
	
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	protected Long id;
	
	@Override
	public Long getId()
	{
		return id;
	}
	
	@Override
	public void setId(Long id)
	{
		this.id = id;
	}
	
	@Persistent
	private Long userId = null;
	
	@Persistent
	private String authToken = null;
	
	@Persistent
	private Date expireTime = new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 7);

	@Persistent
	private String ip;
	
	public void setUserId(Long userId)
	{
		this.userId = userId;
	}

	public Long getUserId()
	{
		return userId;
	}

	public void setAuthToken(String authToken)
	{
		this.authToken = authToken;
	}

	public String getAuthToken()
	{
		return authToken;
	}

	public void setExpireTime(Date expireTime)
	{
		this.expireTime = expireTime;
	}

	public Date getExpireTime()
	{
		return expireTime;
	}

	public void setIp(String ip)
	{
		this.ip = ip;
	}

	public String getIp()
	{
		return ip;
	}
	
	@Override
	public AdminDTOEntry ownAdminDTO()
	{
		String[] data = new String[] {
			getId().toString(),
			getUserId().toString(),
			getAuthToken(),
			getExpireTime().toString(),
			getIp(),
		};
		return new SessionEntryDTO(data);
	}
}
